<?php
namespace Info\Controller;
use Common\Controller\BehaviorController;
use Common\Model\AdModel;

class JumpController extends BehaviorController {


    public function index(){
        echo '';
    }

    /**
     * 调用广告显示
     */
    public function js_show(){
        $aid = I('aid');
        $model = new AdModel();
        $data = $model->find($aid);
        if($data['timeset'] == 1 ){
            if($data['starttime'] < time() || $data['endtime'] > time()) {
                exit('');
            }
        }
        if($data){
            if($data['type'] == 0){
                $adStr = $data['normbody'];
            }else{
                $adUrl = UU('Info/jump/js_event',array('aid'=>$aid));
                $adStr = str_replace(
                    array('[%url%]','[%words%]','[%img%]','[%width%]','[%height%]'),
                    array($adUrl,$data['word'],$data['file_path'],$data['width'],$data['height']),
                    $data['normbody']
                );
            }
            $data['show_times'] ++;
            $model->save($data);
        }else{
            $adStr = '';
        }

        $adStr = addslashes($adStr);
        $str = <<<EOF
<!--
document.write("$adStr");
-->
EOF;
        exit($str);
    }

    /**
     * 广告统计跳转
     */
    public function js_event(){
        $aid = I('aid');
        $model = new AdModel();
        $data = $model->find($aid);
        if($data && !empty($data['url'])){
            $url = $data['url'];
            $data['click_times'] ++;
            $model->save($data);
        }else{
            $url = $this->config['cfg_basehost'];
        }
        header("Location:$url");
    }

}